package demo1;

import java.util.Arrays;

public class MyArrayList implements IList{
    public int[] array;
    public int usedSize;
    public static final int DEFAULT_CAPACITY=10;
    public MyArrayList(){
        this.array=new int[DEFAULT_CAPACITY];
    }

    //usedSize是有效长度，就是有几个具体的数据。
    @Override
    public void add(int data) {
        if (isFull()){
            grow();
        }
        array[usedSize]=data;
        usedSize++;

    }
    private boolean isFull(){
        return this.usedSize==this.array.length;
    }
    private void grow(){
        this.array=Arrays.copyOf(array,this.array.length*2);
    }

    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(this.array[i]);
        }
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    //重点：检查pos的位置是否合法
    public void add(int pos, int data) {
        try{
            checkPos(pos);
            if (isFull()){
                grow();
            }
            for (int i = usedSize; i >pos ; i--) {
                this.array[i]=this.array[i-1];
            }
            this.array[pos]=data;
            usedSize++;
        }catch (PosIllegal e){
            e.printStackTrace();
        }
    }

    public void checkPos (int pos) throws PosIllegal{
        if(pos<0||pos>this.usedSize){
            throw new PosIllegal("pos 位置不合法");
        }
    }

    @Override
    public int get(int pos) {
        try{
            checkPos2(pos);
            empty();
            return this.array[pos];

        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
        //总要返回一个值
        return -1;
    }
    public void checkPos2(int pos) throws PosIllegal{
        if(pos<0||pos>=this.usedSize){
            throw new PosIllegal("pos2位置不合法");
        }
    }
    public void empty() throws EmptyException{
        if(usedSize==0){
            throw new EmptyException("数组为空数组");
        }
    }

    @Override
    public void set(int pos, int value) {
        try{
            checkPos2(pos);
            empty();
            this.array[pos]=value;
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int data) {
        try{
            empty();
            int pos=toRemove(data);
            if(pos==-1){
                return;
            }
            for (int i = pos; i <this.usedSize-1 ; i++) {
                this.array[i]=this.array[i+1];
            }
            this.usedSize--;
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }
    private int toRemove(int data){
        for (int i = 0; i < usedSize; i++) {
            if(array[i]==data){
                return i;
            }

        }
        //当你发现不可以return i的时候，就一定要返回一个别的值，因为咱们这里有返回值类型。
        return -1;
    }

    @Override
    public void clear() {
       this.array=null;
    }
}











